package com.jk.myimkit.imkit.widget.provide;

import android.content.Context;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.TextUtils;
import android.text.method.LinkMovementMethod;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.jk.myimkit.R;
import com.jk.myimkit.imkit.RongContext;
import com.jk.myimkit.imkit.RongIM;
import com.jk.myimkit.imkit.model.ProviderTag;
import com.jk.myimkit.imkit.model.UIMessage;
import com.jk.myimkit.imkit.userInfoCache.RongUserInfoManager;

import io.rong.imlib.model.UserInfo;
import io.rong.message.RecallNotificationMessage;

@ProviderTag(
        messageContent = RecallNotificationMessage.class,
        showPortrait = false,
        showProgress = false,
        showWarning = false,
        centerInHorizontal = true,
        showSummaryWithName = false
)
public class RecallMessageItemProvider extends IContainerItemProvider.MessageProvider<RecallNotificationMessage> {
    public RecallMessageItemProvider() {
    }

    public void onItemClick(View view, int position, RecallNotificationMessage content, UIMessage message) {
    }

    public void bindView(View v, int position, RecallNotificationMessage content, UIMessage message) {
        RecallMessageItemProvider.ViewHolder viewHolder = (RecallMessageItemProvider.ViewHolder)v.getTag();
        if (content != null && message != null) {
            String information;
            if (content.getOperatorId().equals(RongIM.getInstance().getCurrentUserId())) {
                information = RongContext.getInstance().getString(R.string.rc_you_recalled_a_message);
            } else {
                UserInfo userInfo = RongUserInfoManager.getInstance().getUserInfo(content.getOperatorId());
                if (userInfo != null && userInfo.getName() != null) {
                    information = userInfo.getName() + RongContext.getInstance().getString(R.string.rc_recalled_a_message);
                } else {
                    information = content.getOperatorId() + RongContext.getInstance().getString(R.string.rc_recalled_a_message);
                }
            }

            viewHolder.contentTextView.setText(information);
        }

    }

    public void onItemLongClick(View view, int position, RecallNotificationMessage content, UIMessage message) {
    }

    public Spannable getContentSummary(RecallNotificationMessage data) {
        return null;
    }

    public Spannable getContentSummary(Context context, RecallNotificationMessage data) {
        if (data != null && !TextUtils.isEmpty(data.getOperatorId())) {
            String information;
            if (data.getOperatorId().equals(RongIM.getInstance().getCurrentUserId())) {
                information = context.getString(R.string.rc_you_recalled_a_message);
            } else {
                UserInfo userInfo = RongUserInfoManager.getInstance().getUserInfo(data.getOperatorId());
                if (userInfo != null && userInfo.getName() != null) {
                    information = userInfo.getName() + context.getString(R.string.rc_recalled_a_message);
                } else {
                    information = data.getOperatorId() + context.getString(R.string.rc_recalled_a_message);
                }
            }

            return new SpannableString(information);
        } else {
            return null;
        }
    }

    public View newView(Context context, ViewGroup group) {
        View view = LayoutInflater.from(context).inflate(R.layout.rc_item_information_notification_message, (ViewGroup)null);
        RecallMessageItemProvider.ViewHolder viewHolder = new RecallMessageItemProvider.ViewHolder();
        viewHolder.contentTextView = (TextView)view.findViewById(R.id.rc_msg);
        viewHolder.contentTextView.setMovementMethod(LinkMovementMethod.getInstance());
        view.setTag(viewHolder);
        return view;
    }

    private static class ViewHolder {
        TextView contentTextView;

        private ViewHolder() {
        }
    }
}
